package com.ebt.m.proposal_v2.bean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.List;

/**
 * 主附险编辑接口所需传递的参数
 *
 * @author archer.qi
 *         Created on 2017/6/5.
 */
public class ArgProductOption implements Serializable {

    public String productId;

    public String productName;

    public String age; // 非必填

    public String sex; // 非必填 0女1男

    public String profession; // 非必填

    public String birthday; // 非必填 未跟age关联 '2017-6-2'

    public InsuranceOption coverage;

    public InsuranceOption premium;

    public InsuranceOption coveragePeriod;

    public InsuranceOption paymentPeriod;

    public List<InsuranceOption> extraOptions;
    /**
     * 是否是豁免险
     */
    public boolean isExemptRisk;

    public String createOptionsJson() {
        JSONArray arr = new JSONArray();
        try {
            JSONObject coverageObj = new JSONObject();
            coverageObj.put("fieldName", "Coverage");
            coverageObj.put("displayName", coverage == null ? "" : coverage.displayName);
            coverageObj.put("value", coverage == null ? "0" : coverage.value);
            arr.put(coverageObj);

            JSONObject premiumObj = new JSONObject();
            premiumObj.put("fieldName", "Premium");
            premiumObj.put("displayName", premium == null ? "" : premium.displayName);
            premiumObj.put("value", premium == null ? "0" : premium.value);
            arr.put(premiumObj);

            JSONObject coveragePeriodObj = new JSONObject();
            coveragePeriodObj.put("fieldName", "CoveragePeriod");
            coveragePeriodObj.put("displayName", coveragePeriod == null ? "" : coveragePeriod.displayName);
            coveragePeriodObj.put("value", coveragePeriod == null ? "" : coveragePeriod.value);
            arr.put(coveragePeriodObj);

            JSONObject paymentPeriodObj = new JSONObject();
            paymentPeriodObj.put("fieldName", "PaymentPeriod");
            paymentPeriodObj.put("displayName", paymentPeriod == null ? "" : paymentPeriod.displayName);
            paymentPeriodObj.put("value", paymentPeriod == null ? "" : paymentPeriod.value);

            if (extraOptions != null && extraOptions.size() > 0) {
                for (InsuranceOption opt : extraOptions) {
                    if (opt != null) {
                        JSONObject optObj = new JSONObject();
                        optObj.put("fieldName", opt.fieldName);
                        optObj.put("displayName", opt.displayName);
                        optObj.put("value", opt.value);
                        arr.put(optObj);
                    }
                }
            }

            arr.put(paymentPeriodObj);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return arr.toString();
    }
}
